﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Business
{
    public class LogManager
    {
        /// <summary>
        /// 静态类
        /// </summary>
        private LogManager() { }
        private const string LOG_REPOSITORY = "Default"; // this should likely be set in the web config.
        private static ILog m_log;

        /// <summary>
        /// 初始化日志系统
        /// 在系统运行开始初始化
        /// Global.asax Application_Start内
        /// </summary>
        public static void Init()
        {
            log4net.Config.XmlConfigurator.Configure();
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="msg">异常</param>
        public static void WriteErrEventLog(string msg)
        {
            Write(msg, LogMessageType.Error);
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="msg">Info</param>
        public static void WriteInfoEventLog(string msg)
        {
            Write(msg, LogMessageType.Info);
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="message">日志信息</param>
        /// <param name="messageType">日志类型</param>
        public static void Write(string message, LogMessageType messageType)
        {
            DoLog(message, messageType, null, Type.GetType("System.Object"));
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="message">日志信息</param>
        /// <param name="messageType">日志类型</param>
        /// <param name="type"></param>
        public static void Write(string message, LogMessageType messageType, Type type)
        {
            DoLog(message, messageType, null, type);
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="message">日志信息</param>
        /// <param name="messageType">日志类型</param>
        /// <param name="ex">异常</param>
        public static void Write(string message, LogMessageType messageType, Exception ex)
        {
            DoLog(message, messageType, ex, Type.GetType("System.Object"));
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="message">日志信息</param>
        /// <param name="messageType">日志类型</param>
        /// <param name="ex">异常</param>
        /// <param name="type"></param>
        public static void Write(string message, LogMessageType messageType, Exception ex, Type type)
        {
            DoLog(message, messageType, ex, type);
        }

        /// <summary>
        /// 断言
        /// </summary>
        /// <param name="condition">条件</param>
        /// <param name="message">日志信息</param>
        public static void Assert(bool condition, string message)
        {
            Assert(condition, message, Type.GetType("System.Object"));
        }

        /// <summary>
        /// 断言
        /// </summary>
        /// <param name="condition">条件</param>
        /// <param name="message">日志信息</param>
        /// <param name="type">日志类型</param>
        public static void Assert(bool condition, string message, Type type)
        {
            if (condition == false)
                Write(message, LogMessageType.Info);
        }

        /// <summary>
        /// 保存日志
        /// </summary>
        /// <param name="message">日志信息</param>
        /// <param name="messageType">日志类型</param>
        /// <param name="ex">异常</param>
        /// <param name="type">日志类型</param>
        public static void DoLog(string message, LogMessageType messageType, Exception ex, Type type)
        {

            m_log = log4net.LogManager.GetLogger(type);

            switch (messageType)
            {
                case LogMessageType.Debug:
                    LogManager.m_log.Debug(message, ex);
                    break;

                case LogMessageType.Info:
                    LogManager.m_log.Info(message, ex);
                    break;

                case LogMessageType.Warn:
                    LogManager.m_log.Warn(message, ex);
                    break;

                case LogMessageType.Error:
                    LogManager.m_log.Error(message, ex);
                    break;

                case LogMessageType.Fatal:
                    LogManager.m_log.Fatal(message, ex);
                    break;
            }
        }

        /// <summary>
        /// 日志类型
        /// </summary>
        public enum LogMessageType
        {
            /// <summary>
            /// 调试
            /// </summary>
            Debug,
            /// <summary>
            /// 信息
            /// </summary>
            Info,
            /// <summary>
            /// 警告
            /// </summary>
            Warn,
            /// <summary>
            /// 错误
            /// </summary>
            Error,
            /// <summary>
            /// 致命错误
            /// </summary>
            Fatal
        }

    }
}
